package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.change.dispatch.mapper.ChangeOrderLocationMapper;
import com.koron.order.change.util.ThreadLocalOrderChange;
import com.koron.order.common.bean.base.OrderType;
import com.koron.order.common.bean.po.OrderLocationPo;
import com.koron.order.common.mapper.OrderLocationMapper;
import com.koron.order.common.service.OrderLocationService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 工单工作地点表(OrderLocation)表服务实现类
 *
 * @author zhouj
 * @since 2022-05-17 10:07:38
 */
@Service("orderLocationService")
public class OrderLocationServiceImpl implements OrderLocationService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OrderLocationPo orderLocation) {

        return getOrderWorkDeptMapper(factory).insertOrUpdate(orderLocation);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<OrderLocationPo> orderLocationList) {

        return getOrderWorkDeptMapper(factory).insertOrUpdateBatch(orderLocationList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderLocationPo orderLocation) {

        return getOrderWorkDeptMapper(factory).insert(orderLocation);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderLocationPo> orderLocationList) {

        if (CollectionUtils.isEmpty(orderLocationList)) {
            return 0;
        }

        //地点去重
        List<OrderLocationPo> orderLocationPos = orderLocationList.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getLocation() + f.getLocationName()))), ArrayList::new));

        OrderLocationMapper orderWorkDeptMapper = getOrderWorkDeptMapper(factory);

        List<OrderLocationPo> poList = orderWorkDeptMapper.queryByOrderId(orderLocationPos.get(0).getOrderId());
        if(CollectionUtils.isNotEmpty(poList)){
            orderWorkDeptMapper.deleteByOrderIds(poList.stream().map(OrderLocationPo::getId).collect(Collectors.joining(",")));
        }
        return orderWorkDeptMapper.insertBatch(orderLocationPos);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderLocationPo orderLocation) {
        return getOrderWorkDeptMapper(factory).update(orderLocation);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        return getOrderWorkDeptMapper(factory).deleteById(id);
    }

    @Override
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        return getOrderWorkDeptMapper(factory).deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderLocationPo queryById(SessionFactory factory, String id) {

        return getOrderWorkDeptMapper(factory).queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderLocationPo> queryList(SessionFactory factory, OrderLocationPo orderLocation) {

        return getOrderWorkDeptMapper(factory).queryList(orderLocation);
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<OrderLocationPo> queryByOrderId(SessionFactory factory, String orderId) {

        return getOrderWorkDeptMapper(factory).queryByOrderId(orderId);
    }

    @Override
    public List<OrderLocationPo> queryByOrderIds(SessionFactory factory, List<String> orderIds) {

        return getOrderWorkDeptMapper(factory).queryByOrderIds(orderIds);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OrderLocationPo orderLocation, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());

        List<OrderLocationPo> list = getOrderWorkDeptMapper(factory).queryList(orderLocation);

        return new PageInfo(list);
    }

    OrderLocationMapper getOrderWorkDeptMapper(SessionFactory factory) {

        if (OrderType.CHANGE.equals(ThreadLocalOrderChange.get())) {
            return factory.getMapper(ChangeOrderLocationMapper.class);
        }
        return factory.getMapper(OrderLocationMapper.class);
    }

}
